/*
 * Gui.java
 *
 * Created on 29.06.2011, 17:48:36
 */
package de.ret_world.eet;

import de.ret_world.eet.code.Alphabet;
import de.ret_world.eet.code.CodeTable;
import de.ret_world.eet.code.Convert;
import de.ret_world.eet.code.Convert.DataType;
import de.ret_world.eet.code.Symbol;
import de.ret_world.eet.encryption.Decrypt;
import de.ret_world.eet.encryption.Encrypt;
import de.ret_world.eet.encryption.IO;
import de.ret_world.eet.view.CodeTableModel;
import de.ret_world.eet.view.HeaderListModel;
import de.ret_world.eet.view.RowHeaderRenderer;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author Emu
 */
public class Gui extends JFrame implements ClipboardOwner {

    private final CodeTable table;
    private final JList rowHeader;
    private Encrypt encrypt;
    private Decrypt decrypt;

    /** Creates new form Gui */
    public Gui() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }

        Alphabet a = new Alphabet();
        a.parseString("A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s,T,t,U,u,V,v,W,w,X,x,Y,y,Z,z", ",");

        table = new CodeTable(a);

        rowHeader = new JList(new HeaderListModel(a));
        rowHeader.setFixedCellHeight(16);

        Symbol s = table.getSymbol(0, 0);
        int length = 20 + s.getLength();

        rowHeader.setFixedCellWidth(length);

        initComponents();

        for (int i = 0; i < a.getNumberOfSymbols(); i++) {
            tblCodetable.getTableHeader().getColumnModel().getColumn(i).setMaxWidth(length);
            tblCodetable.getTableHeader().getColumnModel().getColumn(i).setMinWidth(length);
        }

        encrypt = new Encrypt(table, false);
        decrypt = new Decrypt(table, false);

        txtEncrypted.setText(encrypt.encrypt(txtPassword.getText(), txtDecrypted.getText()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlDecrypted = new javax.swing.JScrollPane();
        txtDecrypted = new javax.swing.JTextPane();
        btnEncrypt = new javax.swing.JButton();
        btnDecrypt = new javax.swing.JButton();
        scrlCodetable = new javax.swing.JScrollPane();
        tblCodetable = new javax.swing.JTable();
        lblDecrypted = new javax.swing.JLabel();
        lblEncrypted = new javax.swing.JLabel();
        lblCodetable = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        btnRandom = new javax.swing.JButton();
        btnSaveCode = new javax.swing.JButton();
        btnLoadCode = new javax.swing.JButton();
        txtPassword = new javax.swing.JTextField();
        chbOTP = new javax.swing.JCheckBox();
        btnRandomPW = new javax.swing.JButton();
        btnClipboard = new javax.swing.JButton();
        lblReadme = new javax.swing.JLabel();
        lblAlphabet = new javax.swing.JLabel();
        txtAlphabet = new javax.swing.JTextField();
        lblSeperation = new javax.swing.JLabel();
        txtSeperation = new javax.swing.JTextField();
        btnGenerate = new javax.swing.JButton();
        cbxPresets = new javax.swing.JComboBox();
        lblPresets = new javax.swing.JLabel();
        scrlEncrypted = new javax.swing.JScrollPane();
        txtEncrypted = new javax.swing.JTextPane();
        chbRemoveSymbols = new javax.swing.JCheckBox();
        btnLoadText = new javax.swing.JButton();
        btnSaveText = new javax.swing.JButton();
        btnExchange = new javax.swing.JButton();
        btnFill = new javax.swing.JButton();
        lblLicense = new javax.swing.JLabel();
        btnConvert = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Emu's Encryption Tool v1.2");

        txtDecrypted.setText("The quick brown fox jumps over the lazy dog.");
        scrlDecrypted.setViewportView(txtDecrypted);

        btnEncrypt.setText("Encrypt v");
        btnEncrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncryptActionPerformed(evt);
            }
        });

        btnDecrypt.setText("^ Decrypt");
        btnDecrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDecryptActionPerformed(evt);
            }
        });

        scrlCodetable.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrlCodetable.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        tblCodetable.setModel(new CodeTableModel(table));
        tblCodetable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tblCodetable.setEnabled(false);
        tblCodetable.setRowSelectionAllowed(false);
        tblCodetable.getTableHeader().setResizingAllowed(false);
        tblCodetable.getTableHeader().setReorderingAllowed(false);
        rowHeader.setCellRenderer(new RowHeaderRenderer(tblCodetable));
        scrlCodetable.setViewportView(tblCodetable);
        scrlCodetable.setRowHeaderView(rowHeader);

        lblDecrypted.setText("Decrypted/Normal Text");

        lblEncrypted.setText("Encrypted Text");

        lblCodetable.setText("Codetable");

        lblPassword.setText("Password");

        btnRandom.setText("Random");
        btnRandom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRandomActionPerformed(evt);
            }
        });

        btnSaveCode.setText("Save to File");
        btnSaveCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveCodeActionPerformed(evt);
            }
        });

        btnLoadCode.setText("Load from File");
        btnLoadCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadCodeActionPerformed(evt);
            }
        });

        txtPassword.setText("abcd");

        chbOTP.setText("Strong encryption (OTP)");
        chbOTP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbOTPActionPerformed(evt);
            }
        });

        btnRandomPW.setText("Random");
        btnRandomPW.setEnabled(false);
        btnRandomPW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRandomPWActionPerformed(evt);
            }
        });

        btnClipboard.setText("Copy to Clipboard");
        btnClipboard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClipboardActionPerformed(evt);
            }
        });

        lblReadme.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblReadme.setText("If you want to know how the encryption works, read the Readme file!");

        lblAlphabet.setText("Alphabet");

        txtAlphabet.setText("a,b,c,d");

        lblSeperation.setText("Seperation string");

        txtSeperation.setText(",");

        btnGenerate.setText("Generate");
        btnGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerateActionPerformed(evt);
            }
        });

        cbxPresets.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Latin Alphabet", "Alphanumeric", "Bytes (Binary)", "Bytes (Hex)" }));
        cbxPresets.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxPresetsActionPerformed(evt);
            }
        });

        lblPresets.setText("Presets");

        scrlEncrypted.setViewportView(txtEncrypted);

        chbRemoveSymbols.setText("Remove symbols not in the alphabet");
        chbRemoveSymbols.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbRemoveSymbolsActionPerformed(evt);
            }
        });

        btnLoadText.setText("^ Load...");
        btnLoadText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadTextActionPerformed(evt);
            }
        });

        btnSaveText.setText("v Save...");
        btnSaveText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveTextActionPerformed(evt);
            }
        });

        btnExchange.setText("^-v");
        btnExchange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExchangeActionPerformed(evt);
            }
        });

        btnFill.setText("Fill Text");
        btnFill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFillActionPerformed(evt);
            }
        });

        lblLicense.setText("Licensed under GPL. Made by Loofou - http://sourceforge.net/projects/emucrypttool/");

        btnConvert.setText("^ Convert");
        btnConvert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConvertActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrlEncrypted, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                    .addComponent(scrlDecrypted, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnLoadText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEncrypt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDecrypt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnExchange)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnConvert)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSaveText))
                    .addComponent(lblEncrypted, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDecrypted, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblSeperation)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtSeperation, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnGenerate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                        .addComponent(lblPresets, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbxPresets, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(txtAlphabet, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblAlphabet)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnRandom)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 192, Short.MAX_VALUE)
                        .addComponent(btnLoadCode)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSaveCode)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblPassword)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(scrlCodetable, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
                        .addGap(11, 11, 11))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblCodetable)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chbOTP)
                            .addComponent(chbRemoveSymbols))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnFill, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnClipboard, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRandomPW)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblReadme, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblLicense)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDecrypted)
                    .addComponent(lblCodetable))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrlCodetable, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                    .addComponent(scrlDecrypted, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRandom)
                    .addComponent(btnSaveCode)
                    .addComponent(btnLoadCode)
                    .addComponent(btnLoadText)
                    .addComponent(btnEncrypt)
                    .addComponent(btnDecrypt)
                    .addComponent(btnSaveText)
                    .addComponent(btnConvert)
                    .addComponent(btnExchange))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEncrypted, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chbOTP)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnRandomPW)
                                .addComponent(btnClipboard)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(chbRemoveSymbols)
                                .addGap(14, 14, 14)
                                .addComponent(lblAlphabet))
                            .addComponent(btnFill))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAlphabet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblSeperation)
                            .addComponent(txtSeperation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnGenerate)
                            .addComponent(lblPresets)
                            .addComponent(cbxPresets, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblReadme)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(lblLicense))
                    .addComponent(scrlEncrypted, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateActionPerformed
        try {
            Alphabet a = new Alphabet();
            a.parseString(txtAlphabet.getText(), txtSeperation.getText());
            table.setAlphabet(a);
            table.generateNewTable();
            fixTable(a);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnGenerateActionPerformed

    private void fixTable(Alphabet a) {
        tblCodetable.setModel(new CodeTableModel(table));

        Symbol s = table.getSymbol(0, 0);
        int length = 20 + s.getLength() * 5;

        rowHeader.setModel(new HeaderListModel(a));

        rowHeader.setFixedCellWidth(length);

        for (int i = 0; i < a.getNumberOfSymbols(); i++) {
            tblCodetable.getTableHeader().getColumnModel().getColumn(i).setMaxWidth(length);
            tblCodetable.getTableHeader().getColumnModel().getColumn(i).setMinWidth(length);
        }
    }

    private void btnRandomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRandomActionPerformed
        table.generateNewTable();
        fixTable(table.getAlphabet());
    }//GEN-LAST:event_btnRandomActionPerformed

    private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEncryptActionPerformed
        try {
            txtEncrypted.setText(encrypt.encrypt(txtPassword.getText(), txtDecrypted.getText()));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnEncryptActionPerformed

    private void btnDecryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDecryptActionPerformed
        try {
            txtDecrypted.setText(decrypt.decrypt(txtPassword.getText(), txtEncrypted.getText()));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnDecryptActionPerformed

    private void cbxPresetsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxPresetsActionPerformed
        switch (cbxPresets.getSelectedIndex()) {
            case 0:
                txtAlphabet.setText("A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s,T,t,U,u,V,v,W,w,X,x,Y,y,Z,z");
                txtSeperation.setText(",");
                break;
            case 1:
                txtAlphabet.setText("A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s,T,t,U,u,V,v,W,w,X,x,Y,y,Z,z, ,0,1,2,3,4,5,6,7,8,9");
                txtSeperation.setText(",");
                break;
            case 2:
                String s = "";
                for (int i = 0; i < 255; i++) {
                    String tmp = Integer.toBinaryString(i);
                    int diff = 8 - tmp.length();
                    for (int x = 0; x < diff; x++) {
                        tmp = "0" + tmp;
                    }
                    if (i < 255) {
                        s += tmp + ",";
                    } else {
                        s += tmp;
                    }
                }

                txtAlphabet.setText(s.substring(0, s.length() - 1));
                txtSeperation.setText(",");
                break;
            case 3:
                txtAlphabet.setText("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F");
                txtSeperation.setText(",");
                break;
            default:
                break;
        }
    }//GEN-LAST:event_cbxPresetsActionPerformed

    private void chbOTPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbOTPActionPerformed
        boolean on = chbOTP.isSelected();
        btnRandomPW.setEnabled(on);
        txtPassword.setEnabled(!on);

        if (on) {
            btnRandomPWActionPerformed(evt);
        }
    }//GEN-LAST:event_chbOTPActionPerformed

    private void btnRandomPWActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRandomPWActionPerformed
        Random r = new Random();
        int l = table.getSymbol(0, 0).getLength();
        String decr = txtDecrypted.getText();
        List symb = new ArrayList(table.getAlphabet().getSymbols());

        //Text anpassen, damit er vollständig verschlüsselbar ist
//        while(decr.length() % l != 0) {
//            decr += symb.get(0).toString().substring(0, l);
//        }

        //Passwort erstellen
        int tl = (decr.length() / l) + 1;
        String pw = "";
        for (int i = 0; i < tl; i++) {
            pw += symb.get(r.nextInt(symb.size()));
        }
        txtPassword.setText(pw);
        txtDecrypted.setText(decr);
    }//GEN-LAST:event_btnRandomPWActionPerformed

    private void chbRemoveSymbolsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbRemoveSymbolsActionPerformed
        encrypt = new Encrypt(table, chbRemoveSymbols.isSelected());
        decrypt = new Decrypt(table, chbRemoveSymbols.isSelected());
    }//GEN-LAST:event_chbRemoveSymbolsActionPerformed

    private void btnClipboardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClipboardActionPerformed
        try {
            StringSelection stringSelection = new StringSelection(txtPassword.getText());
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(stringSelection, this);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnClipboardActionPerformed

    private void btnExchangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExchangeActionPerformed
        String normal = txtDecrypted.getText();
        txtDecrypted.setText(txtEncrypted.getText());
        txtEncrypted.setText(normal);
    }//GEN-LAST:event_btnExchangeActionPerformed

    private void btnSaveCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveCodeActionPerformed
        try {
            JFileChooser jf = new JFileChooser(new File("."));
            if (JFileChooser.APPROVE_OPTION == jf.showSaveDialog(this)) {
                String sep = JOptionPane.showInputDialog("Set a seperator symbol", ",");
                if (sep != null) {
                    IO.saveStringToFile(jf.getSelectedFile(), table.getCodeTableString(sep));
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnSaveCodeActionPerformed

    private void btnFillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFillActionPerformed
        try {
            int l = table.getSymbol(0, 0).getLength();
            String decr = txtDecrypted.getText();
            List symb = new ArrayList(table.getAlphabet().getSymbols());

            //Text anpassen, damit er vollständig verschlüsselbar ist
            if (decr.length() % l != 0) {
                decr += symb.get(0).toString().substring(0, l - (decr.length() % l));
            }

            txtDecrypted.setText(decr);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnFillActionPerformed

    private void btnLoadTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadTextActionPerformed
        try {
            JFileChooser jf = new JFileChooser(new File("."));
            if (JFileChooser.APPROVE_OPTION == jf.showOpenDialog(this)) {
                String selection = (String) JOptionPane.showInputDialog(this, "Select data format", "Select", JOptionPane.PLAIN_MESSAGE, null, new String[]{"Text", "Binary", "Hex"}, "Text");
                String data = null;
                if (selection != null) {
                    if (selection.equals("Text")) {
                        data = IO.loadFileToString(jf.getSelectedFile());
                    } else if (selection.equals("Binary")) {
                        data = IO.loadFileToBinaryString(jf.getSelectedFile());
                    } else if (selection.equals("Hex")) {
                        data = IO.loadFileToHexString(jf.getSelectedFile());
                    } else {
                        data = txtDecrypted.getText();
                    }
                }

                txtDecrypted.setText(data);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnLoadTextActionPerformed

    private void btnSaveTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveTextActionPerformed
        try {
            JFileChooser jf = new JFileChooser(new File("."));
            if (JFileChooser.APPROVE_OPTION == jf.showSaveDialog(this)) {
                String selection = (String) JOptionPane.showInputDialog(this, "Select data format", "Select", JOptionPane.PLAIN_MESSAGE, null, new String[]{"Text", "Binary", "Hex"}, "Text");
                if (selection != null) {
                    if (selection.equals("Text")) {
                        IO.saveStringToFile(jf.getSelectedFile(), txtEncrypted.getText());
                    } else if (selection.equals("Binary")) {
                        IO.saveBinaryStringToFile(jf.getSelectedFile(), txtEncrypted.getText());
                    } else if (selection.equals("Hex")) {
                        IO.saveHexStringToFile(jf.getSelectedFile(), txtEncrypted.getText());
                    }
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnSaveTextActionPerformed

    private void btnConvertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConvertActionPerformed
        try {
            String selection = (String) JOptionPane.showInputDialog(this, "Select convertion", "Convert...", JOptionPane.PLAIN_MESSAGE, null, new String[]{"UTF->Hex", "UTF->Binary", "Hex->UTF", "Hex->Binary", "Binary->UTF", "Binary->Hex"}, "UTF->Hex");

            if (selection != null) {
                String convertion = Convert.convert(DataType.valueOf(selection.split("->")[0]), DataType.valueOf(selection.split("->")[1]), txtDecrypted.getText());

                txtDecrypted.setText(convertion);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnConvertActionPerformed

    private void btnLoadCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadCodeActionPerformed
        JFileChooser jf = new JFileChooser(new File("."));
        if (JFileChooser.APPROVE_OPTION == jf.showOpenDialog(this)) {
            String s = IO.loadFileToString(jf.getSelectedFile());
            try {
                table.parseCodeTable(s);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
            fixTable(table.getAlphabet());
        }
    }//GEN-LAST:event_btnLoadCodeActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClipboard;
    private javax.swing.JButton btnConvert;
    private javax.swing.JButton btnDecrypt;
    private javax.swing.JButton btnEncrypt;
    private javax.swing.JButton btnExchange;
    private javax.swing.JButton btnFill;
    private javax.swing.JButton btnGenerate;
    private javax.swing.JButton btnLoadCode;
    private javax.swing.JButton btnLoadText;
    private javax.swing.JButton btnRandom;
    private javax.swing.JButton btnRandomPW;
    private javax.swing.JButton btnSaveCode;
    private javax.swing.JButton btnSaveText;
    private javax.swing.JComboBox cbxPresets;
    private javax.swing.JCheckBox chbOTP;
    private javax.swing.JCheckBox chbRemoveSymbols;
    private javax.swing.JLabel lblAlphabet;
    private javax.swing.JLabel lblCodetable;
    private javax.swing.JLabel lblDecrypted;
    private javax.swing.JLabel lblEncrypted;
    private javax.swing.JLabel lblLicense;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPresets;
    private javax.swing.JLabel lblReadme;
    private javax.swing.JLabel lblSeperation;
    private javax.swing.JScrollPane scrlCodetable;
    private javax.swing.JScrollPane scrlDecrypted;
    private javax.swing.JScrollPane scrlEncrypted;
    private javax.swing.JTable tblCodetable;
    private javax.swing.JTextField txtAlphabet;
    private javax.swing.JTextPane txtDecrypted;
    private javax.swing.JTextPane txtEncrypted;
    private javax.swing.JTextField txtPassword;
    private javax.swing.JTextField txtSeperation;
    // End of variables declaration//GEN-END:variables

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        //do nothing
    }
}
